//
//  USIMCmdManager.m
//  us
//
//  Created by karry on 2025/7/11.
//

#import "USIMCmdCustomManager.h"

@implementation USIMCmdCustomManager

+(void)imSendCmdWithAction:(NSString *)cmdAction andExt:(NSDictionary *)ext{
    if (USUser.user.relStatus == 0) {
        return;
    }
    
    
    
    EMCmdMessageBody *body = [[EMCmdMessageBody alloc] initWithAction:cmdAction];
    body.isDeliverOnlineOnly = YES; // 不存消息
    EMChatMessage *msg = [[EMChatMessage alloc] initWithConversationID:USUser.user.relImUserName body:body ext:ext];
    
    [[EMClient sharedClient].chatManager sendMessage:msg progress:nil completion:^(EMChatMessage * _Nullable message, EMError * _Nullable error) {
        if (error) {
            NSLog(@"❌ CMD发送失败：%@", error.errorDescription);
        } else {
            NSLog(@"CMD已发送");
        }
    }];
    
}



+(void)imSendCustomWithEvent:(NSString *)event andCustomExt:(NSDictionary *)customExt{
 
    EMCustomMessageBody *aBody = [[EMCustomMessageBody alloc]initWithEvent:event customExt:customExt];
 
    EMChatMessage *message = [[EMChatMessage alloc]initWithConversationID:USUser.user.relImUserName body:aBody ext:nil];
   
    NSString *title = @"您有一条新消息";
    if ([customExt.allKeys containsObject:@"action_title"]) {
        title = customExt[@"action_title"];
    }
    NSString *content = @"新消息";
    if ([customExt.allKeys containsObject:@"action_content"]) {
        content = customExt[@"action_content"];
    }
     
    message.ext = @{
        @"em_apns_ext":@{
            @"em_push_title": title,
            @"em_push_content": content,
        },
        @"us_system":@(1)//是系统消息，不需要显示在聊天记录里
    };
    
    // 发送消息。
    [[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:nil];

    EMConversation *conversation = [EMClient.sharedClient.chatManager getConversation:USUser.user.relImUserName type:EMConversationTypeChat createIfNotExist:YES];

    [conversation deleteMessageWithId:message.messageId error:nil];

}
 

@end
